package com.xlh.server.protocol.support.json;

import android.os.Handler;

import com.xlh.Utils.GsonManager;
import com.xlh.bean.CenterText;
import com.xlh.bean.ProtocolObject.Window;
import com.xlh.interf.IActivity;
import com.xlh.interf.ITakeMessage;
import com.xlh.thread.MessageBagerTask;

import net.nightwhistler.htmlspanner.HtmlSpanner;

/**
 * Created by Created by mike@Q184377367 on 2018/7/2. on 2018/7/2.
 */

public class WindowServer implements ITakeMessage {

    private IActivity atv;
    private String hasType = "window";
    private Window protccoObj;
    public WindowServer(IActivity atv){
        this.atv = atv;


    }

    @Override
    public void takeMessage(String t,String s){

        if(!t.equals(this.hasType)) return;

        protccoObj =  GsonManager.gsmgr.jsonToAnyObject(s, com.xlh.bean.ProtocolObject.Window.class);

        if(protccoObj==null || protccoObj.getText().equals("")) return;

        atv.getActivity().getApp().fixedThreadPool.execute(
                new MessageBagerTask()
                        .setContext(this.atv.getActivity())
                        .setHandler(atv.getActivity().getRenderDataToCenterListHandler())
                        .setData(protccoObj.getText())
                        .setMessageNumber(0)
        );
    }

    @Override
    public boolean takeMessage(String s) {
        return false;
    }

    @Override
    public void init() {

    }

    @Override
    public void atViewLoadAfterInit() {
        setRenderDataToCenterListHandler();
    }


    //右边场景内容列表框更新
    public void setRenderDataToCenterListHandler(){
        final WindowServer me = this;
        this.atv.getActivity().setRenderDataToCenterListHandler( new Handler() {

            @SuppressWarnings("unchecked")
            public void handleMessage(android.os.Message msg) {
                String d = (String) msg.obj;
                if (!d.equals("") || !d.isEmpty()) {

                    CenterText centerText = new CenterText();


                    centerText.setContent(me.atv.getActivity().getHtmlSpanner().fromHtml(d));

                    if(me.atv.getActivity().getmCenterData().size()>1000){

                        me.atv.getActivity().getmCenterData().clear();

                    }

                    me.atv.getActivity().getmCenterData().add(centerText);

                    me.atv.getActivity().getmCenterAdapter().setData(me.atv.getActivity().getmCenterData());
                    //	mCenterList.setSelection(mCenterData.size() - 1);

                    //注意要用 post的方法确保listview的数据已经加载完毕
                    //该方法会卡顿 因为是启动一个线程....

                    me.atv.getActivity().getmCenterList().post(new Runnable() {
                        @Override
                        public void run() {
                            // Select the last row so it will scroll into view...
                            me.atv.getActivity().getmCenterList().setSelection(me.atv.getActivity().getmCenterData().size() - 1);
                        }
                    });



                }


            }
        });
    }





}
